<?php
//function __autoload($classname){require(SITE_DIR."/core/wizard/".$classname.".class.php");}
class CompanyWizard extends Wizard
{
	var $table = "company";
	var $pfx = "company_";
	var $table_contact;
	var $table_spheres;
	var $table_occupations;
	var $cat;
	var $sphere_main_cat;
	var $occupation_main_cat;
	function __construct(object $db,string $tablename,string $pfx,string $id)
	{
		if(isset($db) and is_object($db)){$this->db = $db;}
		else{global $db;$this->db = $db;}
		if(strval($tablename) != ''){$this->table = mysql_real_escape_string($tablename);}
		if(strval($pfx) != ''){$this->pfx = mysql_real_escape_string($pfx);}
		if(strval($id) != ''){$this->pid = $id;}
		$this->table_contact = $this->pfx.'contacts';
		$this->table_spheres = $this->pfx.'sphere_tree';
		$this->cat = new CompanyCategoryWizard();
		$this->sphere_main_cat = $this->cat->byAlias('sphere','job');
		$this->occupation_main_cat = $this->cat->byAlias('job','job');
		$this->users = new UsersWizard;
		global $db_main;
		$this->users->init($db_main);
	}
	function getCompany($id = 0)
	{
		$item = $this->getItem($id);
		if($item != false)
		{
			//TODO: CompanyWizard::getVacancy->Item->spheres
			$vacancy_spheres = $this->db->fetch_list("SELECT category_id FROM $this->table_spheres WHERE $this->pid = '".$item['id']."'");
			$item['spheres'] = $this->cat->getCatArray($vacancy_spheres);
			//TODO: CompanyWizard::getVacancy->Item->link
			$item['link'] = '/company/'.$item['alias'].'/';
			//TODO: CompanyWizard::getVacancy->Item->logo
			if($item['logo'] == '1')
			{
				$item['logotype']['small'] = UPLOAD.'company/'.$item['id'].'/logo_small.gif';
				$item['logotype']['full'] = UPLOAD.'company/'.$item['id'].'/logo.gif';
			}
			else
			{
				$item['logotype'] = false;
			}
			return $item;
		}
		else return false;
	}
	function getCompanyByAlias($alias)
	{
		if(strval($alias) != '')
		{
			$company = $this->db->fetch_one("SELECT id FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."'");
			if(!empty($company))
			{
				$item = $this->getCompany($company['id']);
				if($item != false)
				{
					return $item;
				}
				else return false;
			}
			else return false;
		}
		else return false;
	}
	function getCompanyVacancies($id,$status = '0')
	{
		$vac = new VacancyWizard($this->db,'vacancy');
		$list_of_vacs = $vac->db->fetch_list("SELECT id FROM $vac->table WHERE $this->pid = '".mysql_real_escape_string($id)."' AND status >= '".mysql_real_escape_string($status)."'");
		if(!empty($list_of_vacs))
		{
			foreach($list_of_vacs as $k=>$v)
			{
				$items[] = $vac->getVacancy($v);
			}
			if(!empty($items)) return $items;
			else return false;
		}
		else return false;
	}
	function companyLink($id,$title,$target)
	{
		$item = $this->getCompany($id);
		if($item != false)
		{
			$link = '<a href="/company/'.$item['alias'].'/"'.(strval($title) != ''?' title="'.$title.'"':' title="'.$item['title'].'"').(strval($target) != ''?' target="'.$target.'"':'').'>'.(strval($title) != ''?' title="'.$title.'"':' title="'.$item['title'].'"').'</a>';
			return $link;
		}
		else return false;
	}
}
?>